/*
-------------------------------------------------------------------
BIE is Copyright 2001-2003 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
package com.webdeninteractive.sbie.v1_1.util.xmlsvelte;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;

public class Element {

    // TODO:
    //   support for document encodings 
    //     -> no: that's a serialization issue, not a document property

    String namespace;
    String name;
    Map attrs;
    String text; // does not support mixed content
    List kids;

    public Element( String name ) {
        init( name, new HashMap(), null );
    }
        
    public Element( String name, String text ) {
        init( name, new HashMap(), text );
    }

    public Element( String name, Map attrs ) {
        init( name, attrs, null );
    }

    public Element( String name, Map attrs, String text ) {
        init( name, attrs, text );
    }

    private void init( String name, Map attrs, String text ) {
        this.name = name;
        this.attrs = attrs;
        this.text = text;
        this.kids = new ArrayList();
    }

    public Element setText( String text ) {
        this.text = text;
        return this;
    }

    public Element addText( String moretext ) {
        if ( text==null ) {
            text = moretext;
        } else {
            text = text + moretext;
        }
        return this;
    }

    public Element addChild( String name ) {
        return doAddChild( name, new HashMap(), null );
    }
        
    public Element addChild( String name, String text ) {
        return doAddChild( name, new HashMap(), text );
    }

    public Element addChild( String name, Map attrs ) {
        return doAddChild( name, attrs, null );
    }

    public Element addChild( String name, Map attrs, String text ) {
        return doAddChild( name, attrs, text );
    }

    private Element doAddChild( String name, Map attrs, String text ) {
        Element child = new Element(name,attrs,text);
        kids.add( child );
        return child;
    }

    /** @return child node added */
    public Element addChild( Element child ) {
        kids.add(child);
        return child;
    }

    public Element setAttribute( String name, String value ) {
        attrs.put( name, value );
        return this;
    }

    public Element setAttributes( Map attrs ) {
        this.attrs = attrs;
        return this;
    }


}
